﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using School.Entities;
namespace School.Pages.Teaching
{
    public partial class TE_Student_Question : System.Web.UI.Page
    {
        private static SchoolEntities db = new SchoolEntities();
        public int Student_Id = 31;
        protected void Page_Load(object sender, EventArgs e)
        {
            //if (LstSchools.Items.Count()> 0)
            //    btnsave.Visible = true;
            //else
            //    btnsave.Visible = false;
            if (!IsPostBack)
                LstSchoolsBind();
        }
        private void LstSchoolsBind()
        {
            var qy = from c in db.VTE_Topic_Question where !(db.TE_Student_Topic_Answer.Any(a => a.Question_Id == c.Question_Id && a.Student_Id == Student_Id)) select c;
            LstSchools.DataSource = qy.ToList();
            LstSchools.DataBind();
        }
        protected void LstSchools_ItemDataBound(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType == ListViewItemType.DataItem)
            {
                EntityDataSource ent = (EntityDataSource)e.Item.FindControl("EdsrdbChoice");
                string Question_id = LstSchools.DataKeys[e.Item.DataItemIndex].Values["Question_Id"].ToString();
                Parameter paramter = new Parameter("Question_Id", TypeCode.Int32, Question_id);
                ent.WhereParameters.Add(paramter);
                RadioButtonList Rdb = (RadioButtonList)e.Item.FindControl("rdbchoice");
                CheckBoxList Ch = (CheckBoxList)e.Item.FindControl("Chchoice");
                RadioButtonList rdblisttrufalse = (RadioButtonList)e.Item.FindControl("RadioButtonList1");
                string Id = LstSchools.DataKeys[e.Item.DataItemIndex].Values["Id"].ToString();
                if (Id == "2")
                {
                    Rdb.Visible = true;
                    Ch.Visible = false;
                    rdblisttrufalse.Visible = false;
                }
                else if (Id == "3")
                {
                    Rdb.Visible = false;
                    Ch.Visible = true;
                    rdblisttrufalse.Visible = false;
                }
                else if (Id == "4")
                {
                    Rdb.Visible = false;
                    Ch.Visible = false;
                    rdblisttrufalse.Visible = true;
                }
            }
        }
        protected void btnsave_Click(object sender, EventArgs e)
        {
            bool re = false;
            foreach (ListViewItem item in LstSchools.Items)
            {
                string Question_id = LstSchools.DataKeys[item.DataItemIndex].Values["Question_Id"].ToString();
                string Id = LstSchools.DataKeys[item.DataItemIndex].Values["Id"].ToString();
                RadioButtonList Rdb = (RadioButtonList)item.FindControl("rdbchoice");
                CheckBoxList Ch = (CheckBoxList)item.FindControl("Chchoice");
                RadioButtonList rdblisttrufalse = (RadioButtonList)item.FindControl("RadioButtonList1");
                if (Id == "2")
                {
                    foreach (ListItem rdb in Rdb.Items)
                    {
                        if (rdb.Selected)
                        {
                            re = School.Teaching.Teaching.Add_TE_Student_Topic_Answer(Student_Id, int.Parse(Question_id), Convert.ToInt32(rdb.Value.ToString()));
                        }
                    }
                }
                else if (Id == "3")
                {

                    foreach (ListItem che in Ch.Items)
                    {
                        if (che.Selected)
                        {
                            re = School.Teaching.Teaching.Add_TE_Student_Topic_Answer(Student_Id, int.Parse(Question_id), Convert.ToInt32(che.Value.ToString()));
                        }
                    }
                }
                else if (Id == "4")
                {
                    foreach (ListItem rdblis in rdblisttrufalse.Items)
                    {
                        if (rdblis.Selected)
                        {
                            re = School.Teaching.Teaching.Add_TE_Student_Topic_Answer(Student_Id, int.Parse(Question_id), Convert.ToInt32(rdblis.Value.ToString()));
                        }
                    }
                }
            }
            if (re)
                LstSchoolsBind();
        }
    }
}